package cn.fl.mcr.service.impl;

import java.util.List;

import cn.cmm.base.framework.constant.BaseConstant;
import cn.fl.mcr.service.IMcrPrpsRateDtlService;
import cn.fl.mcr.transform.McrPrpsRateDtlVOTransform;
import cn.fl.mcr.transform.McrPrpsRateVOTransform;
import cn.fl.mcr.vo.McrPrpsRateDtlVO;
import cn.fl.mcr.vo.McrPrpsRateVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.condition.McrCityOpenCtrlQC;
import cn.lionbridgecapital.loan.model.dto.McrCityOpenCtrlDTO;
import cn.lionbridgecapital.loan.model.dto.McrPrpsRateDTO;
import cn.lionbridgecapital.loan.model.dto.McrPrpsRateDtlDTO;
import cn.lionbridgecapital.loan.service.api.IMcrCityOpenCtrlApiService;
import cn.lionbridgecapital.loan.service.api.IMcrPrpsRateDtlApiService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.exception.BaseException;

/**
 * @Description:  客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class McrPrpsRateDtlServiceImpl implements IMcrPrpsRateDtlService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IMcrPrpsRateDtlService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IMcrPrpsRateDtlApiService mcrPrpsRateDtlApiService;

	@Autowired
	private IMcrCityOpenCtrlApiService mcrCityOpenCtrlApiService;

	@Override
	public McrPrpsRateDtlVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		McrPrpsRateDtlDTO dto = null;//this.自定义ApiService.selectById(id);
		return McrPrpsRateDtlVOTransform.toVO(dto);
	}

	@Override
	public Long add(McrPrpsRateDtlVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.mcrPrpsRateDtlApiService.add(McrPrpsRateDtlVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrPrpsRateDtlApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(McrPrpsRateDtlVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.mcrPrpsRateDtlApiService.modify(McrPrpsRateDtlVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.mcrPrpsRateDtlApiService.searchMcrPrpsRateDtlListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())) {
			List<McrPrpsRateDtlVO> voList = McrPrpsRateDtlVOTransform.toVOList((List<McrPrpsRateDtlDTO>) page.getResultList());
			BaseLogUtils.info(logger, "initList", "等级定价.查询返回数据. DATA:" + JSONObject.toJSONString(voList), CurrentThreadContext.getCurrentUserName());
			for (McrPrpsRateDtlVO vo : voList) {
				if (vo.getCityCode().equals("1111")) {
					vo.setCityName("全部");
				} else {
					McrCityOpenCtrlQC mcrCityOpenCtrlQC = new McrCityOpenCtrlQC();
					mcrCityOpenCtrlQC.setIsDel(BaseConstant.IS_YESNO_NO);
					mcrCityOpenCtrlQC.setIsValid(BaseConstant.IS_YESNO_YES);
					mcrCityOpenCtrlQC.setCityCode(vo.getCityCode());
					List<McrCityOpenCtrlDTO> mcrCityOpenCtrlDTOS = mcrCityOpenCtrlApiService.searchMcrCityOpenCtrlDTOList(mcrCityOpenCtrlQC);
					BaseLogUtils.info(logger, "initList", "等级定价.查询城市编码映射. DATA:" + JSONObject.toJSONString(mcrCityOpenCtrlDTOS), CurrentThreadContext.getCurrentUserName());
					if (CollectionUtils.isNotEmpty(mcrCityOpenCtrlDTOS)) {
						vo.setCityName(mcrCityOpenCtrlDTOS.get(0).getCityName());
					}
				}
				if (vo.getCityCodeLater().equals("1111")) {
					vo.setCityNameLater("全部");
				} else {
					McrCityOpenCtrlQC mcrCityOpenCtrlQC = new McrCityOpenCtrlQC();
					mcrCityOpenCtrlQC.setIsDel(BaseConstant.IS_YESNO_NO);
					mcrCityOpenCtrlQC.setIsValid(BaseConstant.IS_YESNO_YES);
					mcrCityOpenCtrlQC.setCityCode(vo.getCityCodeLater());
					List<McrCityOpenCtrlDTO> mcrCityOpenCtrlDTOS = mcrCityOpenCtrlApiService.searchMcrCityOpenCtrlDTOList(mcrCityOpenCtrlQC);
					BaseLogUtils.info(logger, "initList", "等级定价.查询城市编码映射. DATA:" + JSONObject.toJSONString(mcrCityOpenCtrlDTOS), CurrentThreadContext.getCurrentUserName());
					if (CollectionUtils.isNotEmpty(mcrCityOpenCtrlDTOS)) {
						vo.setCityNameLater(mcrCityOpenCtrlDTOS.get(0).getCityName());
					}
				}
			}
			page.setResultList(voList);
		}
		return page;
	}
}
